package com.shushan.pages;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.shushan.base.Constants;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.exam.ExamActivity;
import com.shushan.exercise.SyncExerciseActivity;
import com.shushan.home.bean.RewardTimeBean;
import com.shushan.shushanbao.R;
import com.shushan.util.Session;

/**
 * @ClassName: ToyAwardDetailsActivity
 * @Desciption: //时间奖励详情页
 * @author: zhangshihao
 * @date: 2018-04-10
 */
public class TimeAwardDetailsActivity extends NavigationLoseActivity implements View.OnClickListener{

    private static final String TAG = TimeAwardDetailsActivity.class.getSimpleName();

    private TextView tvTask;
    private Button btnGoComplete;
    private ListView lvTime;

    private RewardTimeBean rewardTimeBean;
    private int type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_time_award_details);

        setTitle("我的奖品");

        rewardTimeBean = (RewardTimeBean) Session.getSession().get("rewardTimeBean");

        initView();
    }

    private void initView(){
        btnGoComplete = findViewById(R.id.btn_details_go_complete);
        tvTask = findViewById(R.id.tv_complete_condition);
        lvTime = findViewById(R.id.list_view);

        btnGoComplete.setOnClickListener(this);

        if(rewardTimeBean != null){
            type = rewardTimeBean.practice_type;
            lvTime.setAdapter(new MyListAdapter());

            tvTask.setText("完成情况： " + rewardTimeBean.task);
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_details_go_complete){
            switch (type) {
                case Constants.TYPE_SYNC_EXERCISE:
                    Session.getSession().put("rewardTimeBean",rewardTimeBean);

                    Intent intent = new Intent(this,SyncExerciseActivity.class);
                    intent.putExtra("awardType", 1);
                    startActivityAllow(intent);
                    break;
                case Constants.TYPE_SMART_EXERCISE:
                    startSmartExam(rewardTimeBean);
                    break;
                case Constants.TYPE_KEY_POINT_EXERCISE:
                case Constants.TYPE_DIFFICULT_POINT_EXERCISE:
                case Constants.TYPE_EASY_WRONG_EXERCISE:
                case Constants.TYPE_SPECIAL_TOPIC_EXERCISE:
                    Session.getSession().put("rewardTimeBean",rewardTimeBean);

                    Intent intent1 = new Intent(this,SpecialTopicExerciseActivity.class);
                    intent1.putExtra("awardType", 1);
                    startActivityAllow(intent1);
                    break;
            }
        }
    }

    private class MyListAdapter extends BaseAdapter{

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_score_time,null,false);
                //holder.tvScore = convertView.findViewById(R.id.tv_score);
                holder.tvTime = convertView.findViewById(R.id.tv_time);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tvTime.setText(RewardTimeBean.getTimeDes(position,rewardTimeBean.timeList.get(position)));

            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return rewardTimeBean.timeList.get(position);
        }

        @Override
        public int getCount() {
            return rewardTimeBean.timeList.size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class ViewHolder{
            //private TextView tvScore;
            private TextView tvTime;
        }

    }

    private void startSmartExam(RewardTimeBean rewardTimeBean) {
        if(rewardTimeBean == null){
            showToast("智能练习玩手机奖励信息为空");
            return;
        }

        if(!rewardTimeBean.znlx){
            showToast("智能练习暂无题目");
            return;
        }

        onBackAction();

        Session.getSession().put("rewardTimeBean",rewardTimeBean);

        Intent intent = new Intent(this,ExamActivity.class);
        intent.putExtra("awardType",1);
        startActivityAllow(intent);
    }

}
